package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Dish dish = new Dish();
        // 开启事务 合并属性
        dish.setStatus(StatusConstant.DISABLE);
        BeanUtils.copyProperties(setmealDTO, dish);

        // 添加当前套餐 返回新增的 id
        setmealMapper.insert(dish);

        // 新增套餐菜品列表
        List<SetmealDish> setmealDishs = setmealDTO.getSetmealDishes();
        if (setmealDishs != null && setmealDishs.size() > 0) {
            setmealDishs.forEach(setmealDish -> setmealDish.setSetmealId(dish.getId()));
            // 添加当前套餐菜品
            setmealDishMapper.insertBatch(setmealDishs);
        }

    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 开启分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        // 合并属性 修改套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        // 删除套餐菜品列表
        setmealDishMapper.deleteSetmealDishByid(setmealDTO.getId());

        // 修改套餐菜品列表
        List<SetmealDish> setmealDishs = setmealDTO.getSetmealDishes();
        if (setmealDishs != null && setmealDishs.size() > 0) {
            // 重置套餐id
            setmealDishs.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
            // 重新新增
            setmealDishMapper.insertBatch(setmealDishs);
        }
    }

    @Override
    public SetmealVO getSetmeal(Long setmealId) {
        SetmealVO setmealVO = new SetmealVO();
        // 获取套餐信息
        Setmeal setmeal = setmealMapper.selectById(setmealId);
        BeanUtils.copyProperties(setmeal, setmealVO);

        // 获取套餐菜品列表
        List<SetmealDish> setmealDishs = setmealDishMapper.selectBySetmealId(setmealId);
        setmealVO.setSetmealDishes(setmealDishs);

        return setmealVO;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        // 套餐内如果有停售产品 则套餐无法上架
        if (status.equals(StatusConstant.ENABLE)) {
            // 查询套餐菜品列表 进行判断
            List<Dish> setmealDishs = setmealDishMapper.selectSetmealDishById(id);
            setmealDishs.forEach(setmealDish -> {
                // 判断状态
                if (setmealDish.getStatus().equals(StatusConstant.DISABLE)) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }

        setmealMapper.update(setmeal);
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {

        // 判断套餐状态信息
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.selectById(id);
            // 起售中的套餐不能删除
            if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        // 删除套餐信息
        setmealMapper.deleteBatch(ids);

        // 删除套餐菜品信息
        setmealDishMapper.deleteBatch(ids);
    }

    @Override
    public List<Setmeal> listByCategoryId(Integer categoryId) {
        return setmealMapper.selectByCategoryId(categoryId);
    }

    @Override
    public List<DishItemVO> selectUserSetmealDishById(Long setmealId) {
        return setmealDishMapper.selectUserSetmealDishById(setmealId);
    }
}
